package com.sky.haikangdemo.config;

import com.sky.haikangdemo.entity.Gate;
import com.sky.haikangdemo.mapper.GateMapper;
import com.sky.haikangdemo.utils.HikVisionSDKInitUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import javax.annotation.PreDestroy;
import java.util.List;

/**
 * @author yts
 * @description: 海康SDK初始化配置
 */
@Configuration
@Slf4j
public class HikSDKInitializationConfig {

    @Autowired
    private HikVisionSDKInitUtil hikVisionSDKInitUtil;

    @Autowired
    private GateMapper gateMapper;

    /**
     * 等待所有的bean加载完成之后，再初始化海康SDK
     */
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationEvent() {
        log.info("Hikvision SDK configuration initialization startup");
        hikVisionSDKInitUtil.init();
        // 初始化设备信息
        List<Gate> gates = gateMapper.selectAll();
        if (gates != null && gates.size() > 0) {
            PublicStartConfig.gates = gates;
        }
    }


    /**
     * 应用关闭前，清理海康SDK相关资源
     */
    @PreDestroy
    public void appDestroy() {
        hikVisionSDKInitUtil.cleanup();
    }
}
